package com.example.zyfyptlw618.avtivivites;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zyfyptlw618.R;
import com.example.zyfyptlw618.iface.LoginInputListener;
import com.example.zyfyptlw618.model.LoginInputModel;

public class LoginInputActivity extends AppCompatActivity {
    private EditText reg_username;
    private EditText reg_password;
    private EditText telp;
    private Button reg_btn_sure;
    private String username = "", password = "", tel = "";

    private LoginInputListener loginInputlistener = new LoginInputListener() {
        @Override
        public void onResponse(String msg) {
            if (msg.equals("1")) {
                Toast.makeText(LoginInputActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginInputActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }


        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginInputActivity.this, "注册失败" + msg, Toast.LENGTH_SHORT).show();
            Toast.makeText(LoginInputActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_input);
        initView();
    }

    private void initView() {
        reg_username = findViewById(R.id.editText3);
        reg_password = findViewById(R.id.editText4);
        telp = findViewById(R.id.tel);
        reg_btn_sure = findViewById(R.id.button5);

        reg_btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = reg_username.getText().toString();
                password = reg_password.getText().toString();
                tel = telp.getText().toString();

                if (username.isEmpty()) {
                    Toast.makeText(LoginInputActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                } else if (password.isEmpty()) {
                    Toast.makeText(LoginInputActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (tel.isEmpty()) {
                    Toast.makeText(LoginInputActivity.this, "电话号不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    LoginInputModel model = new LoginInputModel();
                    model.getLoginInputResult(username, password, tel, loginInputlistener);
                }
            }
        });
    }
}